{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "50c6f250-f87f-4cba-a988-7ff453044bac",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-26T13:08:11.901151Z",
     "iopub.status.busy": "2022-01-26T13:08:11.900862Z",
     "iopub.status.idle": "2022-01-26T13:08:12.204168Z",
     "shell.execute_reply": "2022-01-26T13:08:12.203582Z",
     "shell.execute_reply.started": "2022-01-26T13:08:11.901084Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "38c9067b-24ec-48bf-8ccb-1911a1c4cf03",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-26T13:09:25.660891Z",
     "iopub.status.busy": "2022-01-26T13:09:25.660423Z",
     "iopub.status.idle": "2022-01-26T13:09:25.667742Z",
     "shell.execute_reply": "2022-01-26T13:09:25.666825Z",
     "shell.execute_reply.started": "2022-01-26T13:09:25.660867Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "(3,)\n",
      "2\n",
      "(2, 3)\n",
      "3\n",
      "(2, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "# 创建多维数组\n",
    "a1=np.array([1,2,3])\n",
    "print(a1.ndim) # 查看数组a1维度\n",
    "print(a1.shape) # 查看数组a1具体形状\n",
    "a2 = np.array([\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6]\n",
    "])\n",
    "print(a2.ndim) # 查看数组a2维度\n",
    "print(a2.shape)# 查看数组a2具体形状\n",
    "a3 = np.array([\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6]\n",
    "    ],\n",
    "    [\n",
    "        [7,8,9],\n",
    "        [10,11,12]\n",
    "    ]\n",
    "])\n",
    "print(a3.ndim) # 查看数组a3维度\n",
    "print(a3.shape) # 查看数组a3具体形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "931d379e-17e9-4aeb-8496-6b51e3d509d3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-26T13:14:40.325648Z",
     "iopub.status.busy": "2022-01-26T13:14:40.325399Z",
     "iopub.status.idle": "2022-01-26T13:14:40.331742Z",
     "shell.execute_reply": "2022-01-26T13:14:40.330997Z",
     "shell.execute_reply.started": "2022-01-26T13:14:40.325621Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5  6]\n",
      " [ 7  8  9 10 11 12]]\n",
      "2\n",
      "(2, 6)\n",
      "[ 1  2  3  4  5  6  7  8  9 10 11 12]\n",
      "1\n",
      "(12,)\n"
     ]
    }
   ],
   "source": [
    "# 改变数组形状\n",
    "a4 = a3.reshape((2,6))\n",
    "print(a4)\n",
    "print(a4.ndim)\n",
    "print(a4.shape)\n",
    "a5 = a3.reshape((12,))\n",
    "# a5 = a3.flatten() 通常用法，直接扁平化\n",
    "print(a5)\n",
    "print(a5.ndim)\n",
    "print(a5.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "eae6d95a-8281-42e7-9e6b-332dce8cc2a8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-26T13:26:40.605366Z",
     "iopub.status.busy": "2022-01-26T13:26:40.605117Z",
     "iopub.status.idle": "2022-01-26T13:26:40.611637Z",
     "shell.execute_reply": "2022-01-26T13:26:40.610651Z",
     "shell.execute_reply.started": "2022-01-26T13:26:40.605339Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a3.size表示a3中有12个数组元素\n",
      "a3.itemsize表示a3中每个元素所占8个字节的内存\n",
      "int64\n",
      "a3.size * a3.itemsize表示a3总共占用了96个字节的内存\n"
     ]
    }
   ],
   "source": [
    "# size-数组元素的个数\n",
    "# itemsize-数组中元素所占字节大小\n",
    "\n",
    "print(\"a3.size表示a3中有{}个数组元素\".format(a3.size))\n",
    "print(\"a3.itemsize表示a3中每个元素所占{}个字节的内存\".format(a3.itemsize))\n",
    "print(a3.dtype)\n",
    "print(\"a3.size * a3.itemsize表示a3总共占用了{}个字节的内存\".format(a3.size * a3.itemsize))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
